package com.iwith.dongmoon.activity;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;

import com.iwith.dongmoon.gsonlist.BoardList;
import com.iwith.dongmoon.gsonlist.BoardListItem;
import com.iwith.dongmoon.listadpater.FourTextListAdapter;
import com.iwith.dongmoon.util.ApiCode;
import com.iwith.dongmoon.util.RequestGson;
import com.iwith.dongmoon.util.ResultCode;
import com.iwith.dongmoon.util.SessionManager;
import com.iwith.dongmoon.util.ThreadInterface;
import com.iwith.dongmoon.util.ThreadTask;

public class Tab_NewsActivity extends Activity implements OnTabChangeListener, OnClickListener {

	private RequestGson requestGson = new RequestGson();

//	private ListView newsOccasionList;
	private ListView newsMeetingList;
//	private ListView newsAdvertiseList;
	private ListView myNewsList;
	private Button updateNewsBt;
	
	private TabHost firstTabHost;
//	private TabHost secondTabHost;
	
	private Context mContext;
	
	private String [] TAB1_ID = { ApiCode.NEWS, ApiCode.FAVORITENEWS };
//	private String [] TAB2_ID = { ApiCode.OCCASIONS, ApiCode.MEETINGS, ApiCode.ADVERTISEMENTS };
	private String [] TAB1_NAME = { "동문소식", "관심소식" };
//	private String [] TAB2_NAME = { "경조사", "모임", "업체홍보" };
	private Integer [] TAB1_RESOURCE = { R.id.tabnews_newslayout, R.id.tabnews_mynewslistview };
//	private Integer [] TAB2_RESOURCE = { R.id.tabnews_news_occasionlistview, R.id.tabnews_news_meetinglistview, R.id.tabnews_news_advertisementlistview };
	
	private SessionManager mSessionManager=null;
	
	private String userid;
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tab_news);
		// TODO Auto-generated method stub
		mSessionManager=new SessionManager(this);
		firstTabHost = (TabHost)findViewById(R.id.tabnews_firsttabhost);
		firstTabHost.setup();

		
		for(int i=0; i<TAB1_ID.length; i++){
			firstTabHost.addTab(firstTabHost.newTabSpec(TAB1_ID[i]).setIndicator(TAB1_NAME[i]).setContent(TAB1_RESOURCE[i]));
			firstTabHost.getTabWidget().getChildAt(i).getLayoutParams().height = 70;
		}
		firstTabHost.setCurrentTab(0);
		
//		secondTabHost = (TabHost) findViewById(R.id.tabnews_newstabhost);
//		secondTabHost.setup();
//		for(int i=0; i<TAB2_ID.length; i++){
//			secondTabHost.addTab(secondTabHost.newTabSpec(TAB2_ID[i]).setIndicator(TAB2_NAME[i]).setContent(TAB2_RESOURCE[i]));
//			secondTabHost.getTabWidget().getChildAt(i).getLayoutParams().height = 70;
//		}
//		
//		secondTabHost.setCurrentTab(0);
	}

	@Override
	public void onResume(){
		super.onResume();
		ThreadTask.onFinishAll();
		Initialize();
	}
	
	@Override
	public void onPause(){
		super.onPause();
		ThreadTask.onFinishAll();
	}

	private void Initialize() {
		mContext=this;
		
		userid=mSessionManager.getUserid();
		
		updateNewsBt = (Button) findViewById(R.id.tabnews_upnewsbt);
//		newsOccasionList = (ListView) findViewById(R.id.tabnews_news_occasionlistview);
		newsMeetingList = (ListView) findViewById(R.id.tabnews_news_meetinglistview);
//		newsAdvertiseList = (ListView) findViewById(R.id.tabnews_news_advertisementlistview);
		myNewsList = (ListView) findViewById(R.id.tabnews_mynewslistview);
		
		if(firstTabHost.getCurrentTabTag().equals(TAB1_ID[0])==true) {
//			if(secondTabHost.getCurrentTabTag().equals(TAB2_ID[0])==true) {
//				setBoardListData(ApiCode.OCCASIONS, newsOccasionList);
//			} else if(secondTabHost.getCurrentTabTag().equals(TAB2_ID[1])==true) {
				setBoardListData(ApiCode.MEETINGS, newsMeetingList);
//			} else if(secondTabHost.getCurrentTabTag().equals(TAB2_ID[2])==true) {
//				setBoardListData(ApiCode.ADVERTISEMENTS, newsAdvertiseList);
//			}
//			secondTabHost.setOnTabChangedListener(this);
		} else if(firstTabHost.getCurrentTabTag().equals(TAB1_ID[1])==true) {
			setBoardListData(ApiCode.FAVORITENEWS, myNewsList);
		}
		
		firstTabHost.setOnTabChangedListener(this);
//		secondTabHost.setOnTabChangedListener(this);
		updateNewsBt.setOnClickListener(this);
	}


	private void setBoardListData(final String code, final ListView listview) {
		ThreadInterface threadInterface = new ThreadInterface() {
			BoardList boardList;

			public void onThread() {
				// TODO Auto-generated method stub
				String api = null;
//				if (code.equals(ApiCode.OCCASIONS) == true) {
//					api = ApiCode.API_OCCASION;
				if (code.equals(ApiCode.MEETINGS) == true) {
					api = ApiCode.API_MEETING;
//				} else if (code.equals(ApiCode.ADVERTISEMENTS) == true) {
//					api = ApiCode.API_ADVERTISEMENT;
				} else if (code.equals(ApiCode.FAVORITENEWS)==true) {
					api = ApiCode.API_FAVORITENEWS+"/"+userid;
				}
				boardList = requestGson.getBoardGsonBySendingAPI(mContext, api,	"GET", "");
			}

			public void onThreadEnd() {
				// TODO Auto-generated method stub

				if (boardList.getResultCode().equals(ResultCode.SUCCESS) == true) {
					ListAdapter listAdapter = new FourTextListAdapter(mContext, R.layout.listitem_fourtext,	(ArrayList<BoardListItem>) boardList.getList());
					listview.setAdapter(listAdapter); 
				}
				else {
					requestGson.alertToast(mContext, code+" 데이터 로딩 실패");
				}

			}
		};

		ThreadTask task = new ThreadTask((Activity)mContext, threadInterface);
		task.execute();
	}

	public void onTabChanged(String tabId) {
		// TODO Auto-generated method stub
		if(tabId.equals(TAB1_ID[0])==true) {
//			if(secondTabHost.getCurrentTabTag().equals(TAB2_ID[0])==true){
//				setBoardListData(ApiCode.OCCASIONS, newsOccasionList);
//			} else if(secondTabHost.getCurrentTabTag().equals(TAB2_ID[1])==true) {
				setBoardListData(ApiCode.MEETINGS, newsMeetingList);
//			} else if(secondTabHost.getCurrentTabTag().equals(TAB2_ID[2])==true) {
//				setBoardListData(ApiCode.ADVERTISEMENTS, newsAdvertiseList);
//			}
		} else if(tabId.equals(TAB1_ID[1])==true) {
			setBoardListData(ApiCode.FAVORITENEWS, myNewsList);
//		} else if(tabId.equals(TAB2_ID[0])==true) {
//			setBoardListData(ApiCode.OCCASIONS, newsOccasionList);
//		} else if(tabId.equals(TAB2_ID[1])==true) {
//			setBoardListData(ApiCode.MEETINGS, newsMeetingList);
//		} else if(tabId.equals(TAB2_ID[2])==true) {
//			setBoardListData(ApiCode.ADVERTISEMENTS, newsAdvertiseList);
		}
	}

	public void onClick(View v) {
		// TODO Auto-generated method stub
		if(v.getId()==updateNewsBt.getId()){
			// TODO Auto-generated method stub
			Intent intent = new Intent(Tab_NewsActivity.this, UpdateNewsActivity.class);
			intent.putExtra("userid", userid);
			intent.putExtra("code", ApiCode.NEWS);
			startActivity(intent);
		}
	}
}
